package com.lyrcsoft.lambda;

/**
 * @author fujc2dev@126.com
 * @date 2024-08-22
 */
import java.util.function.Function;


public class FunctionExample {
    public static void main(String[] args) {
        // 使用Lambda表达式实现Function
        Function<String, Integer> stringToInt = s -> Integer.parseInt(s);

        // 使用apply方法
        Integer result = stringToInt.apply("123");
        System.out.println(result); // 输出: 123


        // 使用andThen方法链式调用
        Function<Integer, String> intToString = Object::toString;
        Function<String, String> stringProcess = stringToInt.andThen(intToString);
        String processedString = stringProcess.apply("456");
        System.out.println(processedString); // 输出: 456

        // 使用compose方法
        Function<String, String> compose = intToString.compose(stringToInt);
        String convertedBack = compose.apply("789");
        System.out.println(convertedBack); // 输出: 789
    }
}